local duelSkill = fk.CreateSkill {

  name = "joy_doupo_duel_skill",

  tags = {  },

}



duelSkill:addEffect("active", {
  name = "joy_doupo_duel_skill",
  prompt = "#joy_doupo_duel_skill",
  mod_target_filter = function(self, player, to_select, selected)
    return user ~= to_select
  end,
  target_filter = function(self, player, to_select, selected)
    if #selected < self:getMaxTargetNum(player, card) then
      return self:modTargetFilter(to_select, selected, player.id, card)
    end
  end,
  on_action = function(self, room, use, finished)
    local from = room:getPlayerById(use.from)
    if not finished then
      use.extra_data = use.extra_data or {}
      use.extra_data.diy_doupo = use.extra_data.diy_doupo or {}
    else
      for _, pid in ipairs(use.extra_data.diy_doupo) do
        if from.dead then break end
        local p = room:getPlayerById(pid)
        if not p.dead and not p:isKongcheng() then
            room:moveCardTo(table.random(p:getCardIds("h")),Card.PlayerHand,from,fk.ReasonPrey,duelSkill.name)
        end
      end
      use.extra_data.diy_doupo = nil
    end
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    local from = effect.from
    local responsers = { to, from }
    local currentTurn = 1
    local currentResponser = to
    while currentResponser:isAlive() do
      local loopTimes = 1
      if effect.fixedResponseTimes then
        local canFix = currentResponser == to
        if effect.fixedAddTimesResponsors then
          canFix = table.contains(effect.fixedAddTimesResponsors, currentResponser.id)
        end
        if canFix then
          if type(effect.fixedResponseTimes) == 'table' then
            loopTimes = effect.fixedResponseTimes["slash"] or 1
          elseif type(effect.fixedResponseTimes) == 'number' then
            loopTimes = effect.fixedResponseTimes
          end
        end
      end
      local cardResponded
      for i = 1, loopTimes do
        cardResponded = room:askForResponse(currentResponser, 'slash', nil, nil, false, nil, effect)
        if cardResponded then
          room:responseCard({
            from = currentResponser.id,
            card = cardResponded,
            responseToEvent = effect,
          })
        else
          break
        end
      end
      if not cardResponded then
        break
      end
      currentTurn = currentTurn % 2 + 1
      currentResponser = responsers[currentTurn]
    end
    if currentResponser ~= from then
      local use = effect
      use.extra_data = use.extra_data or {}
      use.extra_data.diy_doupo = use.extra_data.diy_doupo or {}
      table.insertIfNeed(use.extra_data.diy_doupo,currentResponser.id)
    end
    if currentResponser:isAlive() then
      room:damage({
        from = responsers[currentTurn % 2 + 1],
        to = currentResponser,
        card = effect.card,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = duelSkill.name,
      })
    end
  end
})

return duelSkill